﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
using DTO;

namespace DATA
{
    public class AccountsREP
    {
        
        MODELGENERALDataContext context = new MODELGENERALDataContext();

        public List<AccountDTO> GetAllAccounts()
        {
            List<AccountDTO> acc = new List<AccountDTO>();

            var acounts = from a in context.Accounts
                          select a;
            foreach (var account in acounts)
            {
                AccountDTO a = new AccountDTO();
                a.Address = account.address;
                a.Answer = account.answer;
                a.Avatar = account.avatar;
                a.Birthday = account.birthday;
                a.CreateDate = account.createDate;
                a.Email = account.email;
                a.FullName = account.fullName;
                a.HqID = null;
                a.IsActived = account.isActived;
                a.Password = "";
                a.Phone = account.phone;
                a.Question = null;
                a.UserName = account.userName;
                acc.Add(a);
            }
            return acc;
        }

        public List<AccountDTO> GetActiveAccount()
        {
           List<AccountDTO> acc = new List<AccountDTO>();

            var acounts = from a in context.Accounts
                          where a.isActived ==true
                          select a;
            foreach (var account in acounts)
            {
                AccountDTO a = new AccountDTO();
                a.Address = account.address;
                a.Answer = account.answer;
                a.Avatar = account.avatar;
                a.Birthday = account.birthday;
                a.CreateDate = account.createDate;
                a.Email = account.email;
                a.FullName = account.fullName;
                a.HqID = account.hqID;
                a.IsActived = account.isActived;
                a.Password = "";
                a.Phone = account.phone;
                a.Question = null;
                a.UserName = account.userName;
                acc.Add(a);
            }
            return acc;
        }

        public AccountDTO Login(string uname, string pass)
        {
            AccountDTO accDTO = new AccountDTO();
            try
            {
                Account acc = context.Accounts.Single(a => a.userName == uname && a.password == pass);
                if (acc != null)
                {
                    accDTO.Address = acc.address;
                    accDTO.Answer = acc.answer;
                    accDTO.Avatar = acc.avatar;
                    accDTO.Birthday = acc.birthday;
                    accDTO.CreateDate = acc.createDate;
                    accDTO.Email = acc.email;
                    accDTO.FullName = acc.fullName;
                    accDTO.HqID = acc.hqID;
                    accDTO.IsActived = acc.isActived;
                    accDTO.Password = "";
                    accDTO.Phone = acc.phone;
                    accDTO.UserName = acc.userName;

                    HiddenQuestion hQ = context.HiddenQuestions.Single(h => h.hqID == acc.hqID);
                    accDTO.Question = hQ.question;

                }
            }
            catch (Exception ex)
            {
            }
            return accDTO;
        }


        public bool  AddAccount(Account newAccount)
        {
            Account newacc = new Account();
            Account acc = context.Accounts.Single(c=>c ==newacc);
            try
            {
                context.Accounts.InsertOnSubmit(acc);

                context.SubmitChanges();
            }
            catch (Exception exx)
            {
                return false;
            }
            
            return true;
             
        }
        public bool DeleteAccount(string accountID)
        {
            try
            {
                var acc = context.Accounts.Single(a => a.userName == accountID);
                context.Accounts.DeleteOnSubmit(acc);
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public bool UpdateAccount(Account accountup)
        {
            try
            {
                Account acc = context.Accounts.Single(
                                                        nv => nv.userName == accountup.userName
                                                        );
                acc.userName = accountup.userName;
                acc.phone = accountup.phone;
                acc.password = accountup.password;
                acc.isActived = accountup.isActived;
                acc.hqID = accountup.hqID;
                acc.fullName = accountup.fullName;
                acc.email = accountup.email;
                acc.createDate = accountup.createDate;
                acc.birthday = accountup.birthday;
                acc.avatar = accountup.avatar;
                acc.answer = accountup.answer;
                acc.address = accountup.address;
                context.SubmitChanges();
            }

             catch (System.Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}
